<?php
/**
 * 《我的工资》实现类
 *
 * @author 孙晓晔
 * @version $Id: Work.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/../finance/Salary.class.php');

class SalaryFile extends Salary {
	public $tbl = Constant::tbl_cw_salary_file;
	
	public $data = array(
		'-' => '请选择',
		// '1' => '数据1（南山考勤）',
		'1' => '数据1（南山工资）',
			
		'3' => '数据3（沙井）',
		'4' => '数据4（清溪，时薪制）',

		'5' => '数据5（清溪，月薪制）',
		'6' => '数据6（后勤）',

		'71' => '数据7（南山奖金）',
		'72' => '数据7（沙井奖金）',
		'73' => '数据7（清溪奖金）',
			
		'8' => '奖金',

		'9' => '其它',
	);
	
	public $status = array(
		'-' => '请选择',
		'1' => '等待处理',
		'2' => '正在处理',
		'3' => '处理完成',

		'9' => '其它',
	);
	
	public $orderby = array(
		'-' => '请选择',
		'emp' => '负责人',

		'dept, id' => '部门',
		'date' => '日期',

		'status' => '状态',
		'1' => '创建时间',
		'2' => '更新时间',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);
		
		$controller->assign_by_ref('data_option', $this->data);
	}

	function find($controller, $request) {
		$this->pretreat($controller);

		// $controller->assign('t1', strtotime('-7 day'));
		
		$name = $this->_name_option();
		$controller->assign_by_ref('tt_option', $name);

		$controller->display($request, 'finance/salary/file/find');
		return true;
	}
	
	function _where($request) {
		$name_s = $request['name_s'];
		$type_s = $request['type_s'];
	
		$date1_s = $request['date1_s'];
		$date2_s = $request['date2_s'];
	
		$my = $this->_my();
	
		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}
	
		if (!empty($name_s)) {
			$where .= " AND name LIKE '%" . $name_s . "%'";
		}
		
		if (!empty($type_s) && $type_s <> '-') {
			$where .= " AND type = '" . $type_s . "'";
		}
	
		if (!empty($date1_s)) {
			$where .= " AND date >= '" . $date1_s . "'";
		}
	
		if (!empty($date2_s)) {
			$where .= " AND date < '" . $date2_s . "'";
		}
	
		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);
	
		return $where;
	}
	
	function _my() {
		if ($this->_is_admin()) {
			return '';
		}
	
		$emp = $_SESSION['id'];
	
		$p = array('1'=>'159', '2'=>'160', '3'=>'175');
	
		$a = $this->_audit_office($p);
		if (count($a) > 0) {
			$office = implode(",", $a);
				
			return sprintf(" visible = 1 AND (office in (%s) OR emp = '%s' OR oper = '%s')", $office, $emp, $emp);
		}
	
		$dept = $this->_where_dept($emp);
		if (!empty($dept)) {
			return sprintf(" visible = 1 AND (%s)", $dept);
		}
	
		return sprintf(" visible = 1 AND (emp = '%s' OR oper = '%s')", $emp, $emp);
	}
	
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();		
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'finance/salary/file/list');
		return true;
	}
	
	function add($controller, $request) {
		$this->pretreat($controller);
	
		$name = $this->_name_option();
		$controller->assign_by_ref('tt_option', $name);
	
		$controller->display($request, 'finance/salary/file/add');
		return true;
	}
	
	function addPost($controller, $request) {
		$this->pretreat($controller);
	
		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
		
		$date = $request['date'];
		
		$type = $request['type'];
		$name = $request['name'];
		$content = addslashes($request['content']);	
	
		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(office, dept, emp, date, name, type, content, oper, createtime, ";
		$fmt .= "modifytime) VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $date, $name, $type, $content, $_SESSION['id']);
		// debug($sql);
		$db->query($sql);
	
		$id = $db->insert_id();
		
		$request['id_s'] = $id;
		$request['id'] = $id;
		
		$ret = $this->_insert_salary($request);
		
		$number = $ret['number'];
		$amount = $ret['amount'];
		
		$fmt = "UPDATE %s SET number = '%s', amount = '%s' WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $number, $amount, $id);
		$db->query($sql);
	
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));
	
		$this->findPost($controller, $request);
		return true;
	}
	
	function _insert_salary($request) {
		require_once(dirname(__FILE__) . '/Salary.class.php');
		$aa = new Salary();
	
		return $aa->_insert($request);
	}
	
	function ajax_add($controller, $request) {
		
		// debug(print_r($request, true));
		
		require_once 'HTTP/Upload.php';
		$upload = new HTTP_Upload('en');
		$file = $upload->getFiles('Filedata');
		if (PEAR::isError($file)) {
			debug($file->getMessage());
		}
		
		$id = $this->_insert($request);
		
		$request['id'] = $id;
		
		// $row = $this->_view($id);
		
		$type = $request['type'];
	
		if ($file->isValid()) {
			$filename = $file->upload['tmp_name'];
			debug($filename);
	
			$this->_txt($id, $filename, $type);
			
			$this->_upload2($file, $request);
		} else {
			debug($file->getMessage());
		}
	
		echo "1";
		return true;
	}
	
	function _upload2($file, $request) {
	
		if (empty($this->tbl)) {
			return false;
		}
	
		if (!class_exists("MogileFs")) {
			return false;
		}
	
		$id = $request['id'];
		$oper = $request['oper'];
	
		$this->_files_clean($id);
		
		$filename = $file->getProp("tmp_name");
			
		$name = $file->getProp('name');
		$ext = $file->getProp('ext');
		// $s = sprintf("name = %s, ext = %s", $name, $ext);
		$rnd = rand(100,1000);
		// debug($s);

		$mg = newfs('ydt');
		$key = sprintf("%s_%s_%s_%s_%s.%s", $this->tbl, $id, $oper, time(), $rnd, $ext);
		$key = strtolower($key);
			
		try {
			$s = sprintf("key = %s, filename = %s\n", $key, $filename);
			debug($s);
				
			$mg->put($filename, $key, 'upload');
			$mg->close();
		} catch(MogileFsException $e) {
			debug($e);
		}
	
		echo "1";
		return true;
	}
	
	function _insert($request) {
	
		$date = $request['date'];
	
		$name = $request['name'];
		$type = $request['type'];
	
		$number = $request['number'];
		$amount = $request['amount'];
	
		$content = addslashes($request['content']);
	
		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
	
		$oper = $request['oper'];
	
		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(office, dept, name, type, date, number, amount, content, emp, oper, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $name, $type, $date, $number, $amount, $content, $emp, $oper);
		debug($sql);
		$db->query($sql);
	
		return $db->insert_id();
	}
	
	function _txt($id, $file, $type) {
		
		$row = $this->_view($id);
		
		$name = $row['name'];
		$content = $row['content'];
		
		$oper = $row['oper'];
		
		require_once(dirname(__FILE__) . '/Salary.class.php');
		$aa = new Salary();
		
		$ret = array();
		$i = 1;
		
		$fd = fopen($file, "r");
		while (!feof($fd)) {
			$buf = $this->_to_utf8(fgets($fd));
			// echo $buf;
				
			switch($type) {
				case 1:
					// debug("269");
					$row = $aa->_line_1($id, $name, $oper, $buf);
					break;
				case 2:
					$row = $aa->_line_2($id, $name, $oper, $buf);
					break;
				case 3:
					$row = $aa->_line_3($id, $name, $oper, $buf);
					break;
				case 4:
					$row = $aa->_line_4($id, $name, $oper, $buf);
					break;
				case 5:
					$row = $aa->_line_5($id, $name, $oper, $buf);
					break;
				case 6:
					$row = $aa->_line_6($id, $name, $oper, $buf);
					break;
				case 71:
					$row = $aa->_line_7_ns($id, $name, $oper, $buf);
					break;
				case 72:
					$row = $aa->_line_7_sj($id, $name, $oper, $buf);
					break;
				case 73:
					$row = $aa->_line_7_qx($id, $name, $oper, $buf);
					break;
				case 8:
					$row = $aa->_line_8($id, $name, $oper, $buf);
					break;
				case 9:
					$row = $aa->_line_9($id, $name, $oper, $buf);
					break;
				default:
					debug("default 308");
					break;
			}
			
			if (is_array($row)) {
				$row['no'] = $i++;
				$row['ok'] = true;
				$ret[] = $row;
			}
		}
		
		fclose($fd);
		
		return $ret;
	}
	
	function edit($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		$row = $this->_view($id);
		
		$row['item'] = json_encode($this->_salary_item($row['id']));
	
		$controller->assign_by_ref('result', $row);
	
		$name = $this->_name_option();
		$controller->assign_by_ref('tt_option', $name);
	
		$controller->display($request, 'finance/salary/file/edit');
		return true;
	}
	
	function _salary_item($id, $status = 'all') {
		require_once(dirname(__FILE__) . '/Salary.class.php');
		$aa = new Salary();
	
		$data = $aa->_list($id);
		
		$ret = array();
		while (list($k, $v) = each($data)) {
			
			$t = array();
			
			$t['id'] = $v['id'];
			$t['emp'] = sprintf("%s:%s", $v['emp'], $v['emp_t']);
				
			$t['d2'] = $v['d2'];
			$t['t2'] = $v['t2'];
			$t['ns'] = $v['ns'];
			$t['sj'] = $v['sj'];
			
			$t['content'] = $v['content'];
			$t['createtime'] = $v['createtime'];
			
			$t['ok'] = true;
			
			$ret[] = $t;
		}
		
		return $ret;
	}
	
	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);
	
		// debug(print_r($request, true));
	
		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);
		
		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
	
		$name = $request['name'];
		$type = $request['type'];
		$date = $request['date'];
		
		// $salary = $this->_salary($id);
		
		// $number = count($salary);
		// $amount = $this->_amount_t2('file', $id);
		
		$content = addslashes($request['content']);
		
		$ret = $this->_salary_edit($request);
		$number = $ret['number'];
		$amount = $ret['amount'];
	
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET office = '%s', dept = '%s', emp = '%s', date = '%s', name = '%s', type = '%s', number = '%s', amount = '%s'";
		$fmt .= ", content = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $date, $name, $type, $number, $amount, $content, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}
	
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));
	
		$this->findPost($controller, $request);
		return true;
	}
	
	function _salary($id, $status = 'all') {
		require_once(dirname(__FILE__) . '/Salary.class.php');
		$aa = new Salary();
	
		return $aa->_list($id);
	}
	
	function _salary_edit($request) {
		
		require_once(dirname(__FILE__) . '/Salary.class.php');
		$aa = new Salary();
	
		return $aa->_edit($request);
		// return $aa->_edit_file($id, $name, $type);
	}
	
	function _amount_t2($field, $id) {
		require_once(dirname(__FILE__) . '/Salary.class.php');
		$aa = new Salary();
	
		$row = $aa->_amount($field, $id);
		
		return $row['t2'];
	}
	
	function _update($request) {
	
		$date = $request['date'];
		$name = $request['name'];
	
		$content = addslashes($request['content']);
	
		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
	
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET office = '%s', dept = '%s', name = '%s', date = '%s', content = '%s', emp = '%s', modifytime = NOW() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $name, $date, $content, $emp);
		debug($sql);
		$db->query($sql);
	
		return true;
	}
	
	function import($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		$row = $this->_view($id);
	
		$controller->assign_by_ref('result', $row);
	
		$controller->display($request, 'finance/salary/file/import');
		return true;
	}
	
	function ajax_import($controller, $request) {
	
		// debug(print_r($request, true));
	
		require_once 'HTTP/Upload.php';
		$upload = new HTTP_Upload('en');
		$file = $upload->getFiles('Filedata');
		if (PEAR::isError($file)) {
			debug($file->getMessage());
		}
	
		$id = $request['id'];
		
		// $row = $this->_view($id);
	
		$type = $request['type'];
	
		if ($file->isValid()) {
			$filename = $file->upload['tmp_name'];
			debug($filename);
	
			$data = $this->_txt($id, $filename, $type);
			
			$controller->assign('id', $id);
			
			$controller->assign_by_ref('result', json_encode($data));
			
			// $this->_upload2($file, $request);
		} else {
			debug($file->getMessage());
		}
	
		// echo "1";
		
		$controller->display($request, 'finance/salary/file/preview');
		return true;
	}
	
	function importPost($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		
		$row = $this->_view($id);
	
		$this->_update_salary($row, $request['data']);
	
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));
	
		$this->findPost($controller, $request);
		return true;
	}
	
	function _update_salary($row, $data) {
		require_once(dirname(__FILE__) . '/Salary.class.php');
		$aa = new Salary();
	
		return $aa->_update($row, $data);
	}
	
	function upload($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
	
		$this->_files_clean($id);
	
		$row = $this->_view($id);
		
		$row['files'] = $this->_files($row['id']);
	
		$controller->assign_by_ref('result', $row);
	
		$controller->display($request, 'finance/salary/file/upload');
		return true;
	}
	
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);
		
		$row['item'] = $this->_salary_list($row['id']);
		
		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'finance/salary/file/view');
		return true;
	}
	
	function _salary_list($id, $status = 'all') {
		require_once(dirname(__FILE__) . '/Salary.class.php');
		$aa = new Salary();
	
		return $aa->_list($id);
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row['type_t'] = $this->type[$row['type']];
 	
		$row = $this->_view_t($row);
	
		// debug(print_r($row, true));
	
		return $row;
	}
	
	function _list() {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE status IN (1, 2) ORDER BY id DESC";
		$sql = sprintf($fmt, $this->tbl);
		debug($sql);
		$result = $db->query($sql);
		$ret = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);
				
			// $ret[$row['id']] = $row['id'] . "/" . $row['customer_t'] . "/" . $row['amount'];
			$ret[] = $row;
		}
	
		return $ret;
	}
	
	function t($controller, $request) {
		$db = newdb($this->dsn_w);
	
		$dd = date("Y-m-d", strtotime("-3 day"));
	
		$fmt = "SELECT * FROM %s";
		$sql = sprintf($fmt, $this->tbl);
		echo $sql . "\n";
		$result = $db->query($sql);
	
		while ($row = $db->fetch_array($result)) {
			print_r($row);
				
			$salary = $this->_salary($row['id']);
		
			$number = count($salary);
			$amount = $this->_amount_t2('file', $row['id']);
				
			$fmt = "UPDATE %s SET number = '%s', amount = '%s' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $number, $amount, $row['id']);
			echo $sql . "\n";
			$db->query($sql);
		}
	
		echo "\n" . $_REQUEST['do'] . "\n";
	
		logdate($controller, $request);
		return true;
	}
}
?>